package com.wzh.musichome.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;


public final class ResponseTemplate {
	private static final String ONE_DATA_KEY = "oneSimpleData";

	private ResponseTemplate() {
	}

	public static final ResultData getFailureResponse(String code, String msg) {
		return new ResultData(false, code, msg);
	}

	public static final ResultData getFailureResponse(ResultCode resultCode) {
		return new ResultData(resultCode);
	}
	public static final ResultData getSuccessResponse() {
		return new ResultData(true, ConstantCode.RESPONSE_SUCCESS, "");
	}

	public static final ResultData getSuccessResponse(ResultCode resultCode) {
		return new ResultData(resultCode);
	}

	public static final ResultData getResponseData(boolean flag, String code, String msg){
		return new ResultData(flag, code, msg);
	}

	public static class ResultData {
		@ApiModelProperty(value="状态", position=0)
		private boolean flag;

		@ApiModelProperty(value="返回码", position=1)
		private String code;

		@ApiModelProperty(value="消息", position=2)
		private String msg;

		@ApiModelProperty(value="数据", position=3)
		private Map<String, Object> data;

		public ResultData(ResultCode resultCode){
			this.flag = resultCode.flag();
			this.code = resultCode.code();
			this.msg = resultCode.msg();
		}

		private ResultData(boolean flag, String code, String msg) {
			this.flag = flag;
			this.code = code;
			this.msg = msg;
		}

		@JsonInclude(JsonInclude.Include.NON_EMPTY)
		public Object getData() {
			if (null == data) {
				return null;
			}
			if (data.containsKey(ONE_DATA_KEY)) {
				return data.get(ONE_DATA_KEY);
			} else {
				return data;
			}
		}

		public void setData(Object data) {
			if (null == this.data) {
				this.data = new HashMap<String, Object>();
			}
			this.data.put(ONE_DATA_KEY, data);
		}

		@JsonInclude(JsonInclude.Include.NON_EMPTY)
		public String getmsg() {
			return msg;
		}

		public void setMsg(String msg) {
			this.msg = msg;
		}

		@JsonInclude(JsonInclude.Include.NON_EMPTY)
		public boolean getFlag() {
			return flag;
		}

		public void setFlag(boolean result) {
			this.flag = result;
		}

		@JsonInclude(JsonInclude.Include.NON_EMPTY)
		public String getCode() {
			return code;
		}

		public void setCode(String code) {
			this.code = code;
		}
	}
}
